package com.geh.learn.algorithm;

public class BinSearch {

    public static void main(String[] args) {
        int a[] = {0, 1,2,3,4,5,6,7,8,9,10};
        System.out.print(search(a, 2));
    }

    private static int search(int a[], int t) {
        // 左边界变量
        int left = 0;
        // 右边界变量
        int right = a.length - 1;
        // 中位数变量
        int middle;
        while (left <= right) {
            middle = (left + right) / 2;
            if (t < a[middle]) {
                // 查找前半段
                right = middle - 1;

            } else if (t > a[middle]) {
                // 查找后半段
                left = middle + 1;

            } else if (t == a[middle]) {

                return middle;
            }
        }
        return -1;
    }

}
